package com.houserent.view;

import com.houserent.domain.House;
import com.houserent.service.HouseService;
import com.houserent.utils.Utility;


/**
 * 1.显示界面
 * 2.接收用户的输入
 * 3.调用 HouseService 完成对房屋信息的各种操作
 */
public class HouseView {

    private boolean loop = true;//控制显示菜单
    private char key = ' ';//接收用户选择的是哪个菜单
    private HouseService houseService = new HouseService(10);//设置数组的大小为10


    //显示主菜单
    public void mainView() {
        do {
            System.out.println("\n=============房屋出租系统=============");
            System.out.println("\t\t\t1.新 增 房 源");
            System.out.println("\t\t\t2.查 找 房 屋");
            System.out.println("\t\t\t3.删 除 房 屋 信 息");
            System.out.println("\t\t\t4.修 改 房 屋 信 息");
            System.out.println("\t\t\t5.显 示 房 屋 信 息");
            System.out.println("\t\t\t6.退            出");
            System.out.print("请输入您的选择(1-6):");
            key = Utility.readChar();
            switch (key) {
                case '1' :
                    addHouse();
                    break;
                case '2' :
                    readHouse();
                    break;
                case '3' :
                    delHouse();
                    break;
                case '4' :
                    updateHouse();
                    break;
                case '5' :
                    listHouses();
                    break;
                case '6' :
                    exit();
                    break;
                default:
                    System.out.println("输入有误");
            }
        } while (loop);
    }

    //修改房屋信息(根据 id)
    public void updateHouse() {
        System.out.print("请输入你要修改的房屋信息的id:");
        int updateId = Utility.readInt();
        //先判断该房屋是否存在
        if (houseService.read(updateId) == null) {
            System.out.println("无该房屋信息，无法修改");
            return;
        }

        //修改房主界面
        while (true) {
            System.out.print("是否修改房屋房主信息(Y/N)：");
            char choice = Utility.readChar();
            if (choice == 'Y') {
                System.out.print("请输入你修改后的信息:");
                houseService.updateOwner(houseService.read(updateId), Utility.readString(8));
                break;
            } else if (choice != 'N') {
                System.out.println("输入有误请重新输入");
                continue;
            }

            break;
        }

        //修改电话界面
        while (true) {
            System.out.print("是否修改房电话信息(Y/N)：");
            char choice = Utility.readChar();
            if (choice == 'Y') {
                System.out.print("请输入你修改后的信息:");
                houseService.updatePhone(houseService.read(updateId), Utility.readString(12));
                break;
            } else if (choice != 'N') {
                System.out.println("输入有误请重新输入");
                continue;
            }

            break;
        }

        //修改地址界面
        while (true) {
            System.out.print("是否修改房屋地址信息(Y/N)：");
            char choice = Utility.readChar();
            if (choice == 'Y') {
                System.out.print("请输入你修改后的信息:");
                houseService.updateAddress(houseService.read(updateId), Utility.readString(16));
                break;
            } else if (choice != 'N') {
                System.out.println("输入有误请重新输入");
                continue;
            }

            break;
        }

        //修改月租界面
        while (true) {
            System.out.print("是否修改房屋月租信息(Y/N)：");
            char choice = Utility.readChar();
            if (choice == 'Y') {
                System.out.print("请输入你修改后的信息:");
                houseService.updateRent(houseService.read(updateId), Utility.readInt());
                break;
            } else if (choice != 'N') {
                System.out.println("输入有误请重新输入");
                continue;
            }

            break;
        }

        //修改状态界面
        while (true) {
            System.out.print("是否修改房屋状态信息(Y/N)：");
            char choice = Utility.readChar();
            if (choice == 'Y') {
                System.out.print("请输入你修改后的信息:");
                houseService.updateState(houseService.read(updateId), Utility.readString(3));
                break;
            } else if (choice != 'N') {
                System.out.println("输入有误请重新输入");
                continue;
            }

            break;
        }

        System.out.println("============房屋信息修改成功============");

    }

    //查找房屋信息(根据 id)
    public void readHouse() {
        System.out.print("请输入你要查的房屋id:");
        int readId = Utility.readInt();
        //判断是否有该房屋信息
        if (houseService.read(readId) == null) {
            System.out.println("无该房屋信息");
            return;
        }

        //输出该房屋信息
        System.out.println("该房屋信息为:");
        System.out.println(houseService.read(readId).toString());

    }


    //显示房屋信息
    public void listHouses() {
        System.out.println("===============房屋信息===============");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");
        House[] houses = houseService.list();//得到所有房屋信息
        for (int i = 0; i < houseService.getCount(); i++) {
            System.out.println(houses[i]);
        }
        System.out.println("============房屋信息显示完毕============");
    }

    //新增房源
    public void addHouse() {
        System.out.println("=============添加房屋=============");
        House house = new House();
        System.out.print("姓名:");
        house.setOwner(Utility.readString(8));
        System.out.print("电话:");
        house.setPhone(Utility.readString(12));
        System.out.print("地址:");
        house.setAddress(Utility.readString(16));
        System.out.print("月租:");
        house.setRent(Utility.readInt());
        System.out.print("状态(未出租/已出租):");
        house.setState(Utility.readString(3));

        if (houseService.add(house)) {
            System.out.println("==============添加房屋成功==============");
        } else {
            System.out.println("==============添加房屋失败==============");
        }
    }

    //删除房屋信息
    public void delHouse() {
        System.out.print("请选择删除房屋编号(-1退出):");
        int delId = Utility.readInt();
        //判断是否退出删除房屋信息操作
        if (delId == -1) {
            return;
        }

        System.out.print("确认是否删除，请小心选择(Y/N):");
        char key = Utility.readChar();
        //再次判断是否退出删除房屋信息操作
        if (key == 'N') {
            return;
        }

        if (houseService.delet(delId)) {
            System.out.println("==============删除房屋成功==============");
        } else {
            System.out.println("==============删除房屋失败==============");
        }
    }

    //退出
    public void exit() {
        //这里我们使用 Utility 提供方法，完成确认
        char c = Utility.readConfirmSelection();
        if (c == 'Y') {
            loop = false;
        }
    }

}